package com.fau.fm.action.index;

import java.io.File;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalFinancialOrders;
import com.fau.fm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 对账Action
 * @author liy
 * @datetime 2014年11月14日,下午1:22:51
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CheckingAction extends PublicAction implements ModelDriven<MedicalFinancialOrders>{

	private static final long serialVersionUID = -8680895962396746340L;
	
	private MedicalFinancialOrders order = new MedicalFinancialOrders();
	
	/**
	 * @desc 进入待对账页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午1:24:12
	 */
	@Action(value = "pending_checking_init", results = {@Result(location = "pending_checking_init.jsp")})
	public String pendingCheckingInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 线下汇款
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午8:18:52
	 */
	@Action(value = "the_line_remittance_init", results = {@Result(location = "the_line_remittance_init.jsp")})
	public String theLineRemittanceInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 货到付款
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午8:18:52
	 */
	@Action(value = "cash_on_delivery_init", results = {@Result(location = "cash_on_delivery_init.jsp")})
	public String cashOnDeliveryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 对账——查看订单明细
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:36:05
	 */
	@Action(value = "order_detail_open_view", results = {@Result(name = SUCCESS, location = "order_detail_open_view.jsp")})
	public String orderDetailOpenView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载待对账页面数据
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午4:20:14
	 */
	@Action(value = "pending_checking_view")
	public String pendingCheckingView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllFinancialByPage(map, order, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 进入待回款页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午1:24:12
	 */
	@Action(value = "pending_checking_recon_init", results = {@Result(location = "pending_checking_recon_init.jsp")})
	public String pendingCheckingReconInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 线下汇款（回款）
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午8:18:52
	 */
	@Action(value = "the_line_remittance_recon_init", results = {@Result(location = "the_line_remittance_recon_init.jsp")})
	public String theLineRemittanceReconInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 货到付款（回款）
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月21日,下午8:18:52
	 */
	@Action(value = "cash_on_delivery_recon_init", results = {@Result(location = "cash_on_delivery_recon_init.jsp")})
	public String cashOnDeliveryReconInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 回款——查看订单明细
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:36:05
	 */
	@Action(value = "order_detail_recon_view", results = {@Result(name = SUCCESS, location = "order_detail_recon_view.jsp")})
	public String orderDetailReconView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载待回款页面数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月21日,下午4:20:14
	 */
	@Action(value = "pending_recon_view")
	public String pendingReconView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllReconByPage(map, order, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 已对账、已回款查看订单明细
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:36:05
	 */
	@Action(value = "order_detail_view_end", results = {@Result(name = SUCCESS, location = "order_detail_view_end.jsp")})
	public String orderDetailView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量导入待对账订单(货到付款)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "batch_checking_import", results = {@Result(name = SUCCESS, location = "batch_checking_import.jsp")})
	public String batchCheckingImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改待对账订单(货到付款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "batch_checking_orders")
	public String batchChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importDelivery(admin, file, SystemConfig.WAIT_RECONCILED_CASH);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入待对账订单(线下汇款)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "batch_check_line_import", results = {@Result(name = SUCCESS, location = "batch_check_line_import.jsp")})
	public String batchCheckLineImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改待对账订单(线下汇款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "batch_line_checking_orders")
	public String batchLineChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importLineDelivery(admin, file, SystemConfig.WAIT_RECONCILED_LINE);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入待回款订单(货到付款)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "batch_recon_import", results = {@Result(name = SUCCESS, location = "batch_recon_import.jsp")})
	public String batchReconImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改待回款订单(货到付款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "batch_recon_orders")
	public String batchRecon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importReconDelivery(admin, file, SystemConfig.WAIT_SECTION_CASH);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入待回款订单(线下汇款)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "batch_recon_line_import", results = {@Result(name = SUCCESS, location = "batch_recon_line_import.jsp")})
	public String batchReconLineImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改待回款订单(线下汇款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "batch_line_recon_orders")
	public String batchLineRecon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info = "";
		try {
			info = this.baseAllService.getCheckingService().importLineReconDelivery(admin, file, SystemConfig.WAIT_SECTION_LINE);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入操作返回信息
	 * @return SUCCESS
	 * @author dangjh
	 * 2015年1月6日 下午2:44:36
	 */
	@Action(value = "orders_batch_warning_info", results = {@Result(name = SUCCESS, location = "orders_batch_warning_info.jsp")})
	public String importResultInfo() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开订单日志记录信息
	 * @return SUCCESS
	 * @author dangjh
	 * 2015年1月12日 下午8:24:49
	 */
	@Action(value = "init_orders_logs_info", results = {@Result(name = SUCCESS, location = "init_orders_logs_info.jsp")})
	public String initOrdersLogs() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载订单日记记录信息数据
	 * @return null
	 * @author dangjh
	 * 2015年1月12日 下午8:32:23
	 */
	@Action(value = "orders_logs_list")
	public String ordersLogsList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getCheckingService().findAllOrdersLog(admin.getEnterpriseId(), order.getSerialNum());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalFinancialOrders getModel() {
		return order;
	}

}
